package Test;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;
import java.util.Scanner;

/**
 * Created by 10188 on 2018/3/29.
 */
public class UDPserver {

    public static void main(String[] args) {
        try (DatagramSocket socket = new DatagramSocket(5021, InetAddress.getByName("192.168.31.221"))) {
            while (true) {
                try {
                    DatagramPacket request = new DatagramPacket(new byte[1024], 1024);
                    socket.receive(request);
                    System.out.println(new String(request.getData()));
                    InetAddress inetAddress = InetAddress.getLocalHost();
                    String hostIEEE="QiLin"+getHostIEEE()+"192.168.31.221";
                    byte data[] = hostIEEE.getBytes();
                    DatagramPacket response = new DatagramPacket(data, data.length, request.getAddress(), request.getPort());
                    socket.send(response);
                    //System.out.println(daytime + " " + request.getAddress());
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private static String getHostIEEE (){
        try {
            long start = System.currentTimeMillis();
            Process process = Runtime.getRuntime().exec(
                    new String[] { "wmic", "cpu", "get", "ProcessorId" });
            process.getOutputStream().close();
            Scanner sc = new Scanner(process.getInputStream());
            String property = sc.next();
            String serial = sc.next();
//                System.out.println(property + ": " + serial);
//                System.out.println("time:" + (System.currentTimeMillis() - start));
            return serial;
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
    }
